class Solution {
public:
    bool isValid(string s) {
        stack<char> mystack;

        for (auto ch : s)
        {
            if (ch == '(' || ch == '[' || ch == '{')
                mystack.push(ch);
            else
            {
                if (mystack.empty())
                    return false;
                
                if (ch == ')' && mystack.top() != '(' ||
                    ch == ']' && mystack.top() != '[' || 
                    ch == '}' && mystack.top() != '{')
                    {
                        return false;
                    }

                mystack.pop();
            }
        }

        return mystack.empty();
    }
};